<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-refresh"
        @click="handleFilter"
      >{{ $t('common.action.refresh') }}</el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-plus"
        @click="handleCreate"
      >{{ $t('organization.action.add') }}</el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      row-key="id"
      :data="list"
      border
      stripe
      fit
      default-expand-all
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column prop="name" :label="$t('organization.name')" />
      <el-table-column prop="sort" :label="$t('organization.sort')" />
      <el-table-column :label="$t('common.options')">
        <template slot-scope="scope">
          <el-button
            slot="reference"
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
          >{{ $t('organization.action.delete') }}</el-button>

          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
          >{{ $t('organization.action.edit') }}</el-button>

          <el-button
            size="mini"
            @click="handleAddNewNode(scope.$index, scope.row)"
          >{{ $t('organization.action.add_node') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="100px"
      >
        <el-form-item :label="$t('organization.name')" prop="title">
          <el-input v-model="temp.title" />
        </el-form-item>
        <el-form-item :label="$t('organization.parentId')" prop="parentId">
          <select-tree v-model="temp.parentId" :options="selectTreeData" :props="defaultProps" />
        </el-form-item>
        <el-form-item :label="$t('organization.sort')" prop="sord">
          <el-input-number v-model="temp.sort" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{ $t('common.action.close') }}</el-button>
        <el-button
          type="primary"
          @click="dialogStatus==='create'?createData():updateData()"
        >{{ $t('common.action.confirm') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { fetchList, add, detail, del, modify } from '@/api/system/organization'
import waves from '@/directive/waves' // waves directive
import SelectTree from '@/components/SelectTree.vue'

export default {
  directives: { waves },
  components: {
    SelectTree
  },
  data() {
    return {

      // 表格
      tableKey: 0,
      listLoading: true,
      list: [],

      // 查询
      listQuery: {
        title: null
      },

      // 对话框
      textMap: {
        create: this.$t('organization.action.add'),
        exit: this.$t('organization.action.edit')
      },
      dialogStatus: '',
      dialogFormVisible: false,
      temp: {
        id: undefined,
        remark: '',
        title: '',
        sort: 1,
        parentId: '-1'
      },
      rules: {
        title: [{ required: true, message: this.$t('validate.notNull'), trigger: 'blur' }],
        parentId: [{ required: true, message: this.$t('validate.notNull'), trigger: 'blur' }]
      },
      defaultProps: {
        parent: 'parentId', // 父级唯一标识
        value: 'id', // 唯一标识
        label: 'name', // 标签显示
        children: 'children' // 子级
      },
      selectTreeData: null

    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleFilter() {
      this.getList()
    },
    getList() {
      this.listLoading = true
      fetchList().then(response => {
        this.list = response.data
        this.selectTreeData = [{
          parentId: null,
          id: '-1',
          name: '根节点',
          children: this.list
        }]
        this.listLoading = false
      })
    },
    handleAddNewNode(index, row) {
      this.temp = {
        id: null,
        title: '',
        sort: 1,
        parentId: row.id
      }
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleEdit(index, row) {
      detail(row.id).then(response => {
        this.temp = {
          id: response.data.id,
          title: response.data.name,
          sort: response.data.sort,
          parentId: response.data.parentId
        }
      })
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleDelete(index, row) {
      this.$confirm(this.$t('common.confirm_delete'), this.$t('common.tips'), {
        confirmButtonText: this.$t('common.action.confirm'),
        cancelButtonText: this.$t('common.action.cancel'),
        type: 'warning'
      }).then(() => {
        this.apiDelterOrg(row.id)
      })
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        title: '',
        sort: 1,
        parentId: '-1'
      }
    },
    apiDelterOrg(id) {
      del(id).then(response => {
        this.getList()
        this.$notify({
          title: this.$t('notify.success'),
          message: this.$t('common.options_success'),
          type: 'success',
          duration: 2000
        })
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          add(this.temp.parentId, this.temp.title, this.temp.sort).then(response => {
            this.dialogFormVisible = false
            this.getList()
            this.$notify({
              title: this.$t('notify.success'),
              message: this.$t('common.options_success'),
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          modify(this.temp.id, this.temp.parentId, this.temp.title, this.temp.sort).then(response => {
            this.dialogFormVisible = false
            this.getList()
            this.$notify({
              title: this.$t('notify.success'),
              message: this.$t('common.options_success'),
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    }
  }
}
</script>
